package test;

import util.StringUtil;
import util.WriteToFileUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * Created by MIC on 2018/11/15.
 */
public class ExceptionData {
    public static void main(String[] args) {
        String path = "Exception-1121.txt";
        Map<String,List> Bmap= new HashMap<String,List>();
        Map<String,List> map = new HashMap<String, List>();
        try{
            FileReader reader = new FileReader(path);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                if(!line.contains(" -> ")){
                    Bmap.put(line.replace("\"",""),null);
                    map.put(line.replace("\"",""),null);
                }else{
                    String[] r=line.replace("\"","").split(" -> ");
                    List<String> list;
                    if(Bmap.get(r[0])!=null){
                        list = Bmap.get(r[0]);
                    }else list = new ArrayList<String>();
                    list.add(r[1]);
                    Bmap.put(r[0],list);

                    List<String> list1;
                    if(map.get(r[1])!=null){
                        list1 = map.get(r[1]);
                    }else list1 = new ArrayList<String>();
                    list1.add(r[0]);
                    map.put(r[1],list1);
                }
            }
            br.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        for(String key : map.keySet()){
            if(map.get(key)==null) {
                List<String> temp = new ArrayList<String>();
                temp.add(key);
                System.out.println("first:"+key);
                List<List<String>> result = new ArrayList<List<String>>();
                getExceptionLink(key, Bmap, temp, result);
                System.out.println(result);
                WriteToFileUtil.appendWrite("DataSet-test.txt",result.toString());
            }

        }
//        WriteToFileUtil.appendWrite("Exception-test.txt",getExceptionLink(s));
    }
    public static void getExceptionLink(String key, Map<String,List> Bmap, List<String> temp, List<List<String>> result){
        if(Bmap.get(key)==null) {
            result.add(new ArrayList<String>(temp));
            return;
        }
        else{
            for(Object s : Bmap.get(key)){
                System.out.println(s);
                temp.add(String.valueOf(s));
                getExceptionLink(String.valueOf(s),Bmap,temp,result);
                temp.remove(temp.size()-1);
            }
        }
    }

}
